/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mbc.common.utils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author kmb
 */
public class EmailUtil {
	
	private static class EmailThread extends Thread{
		private String to;
		private String subject;
		private String message;
		private String user;
		private String password; 
		private boolean isHTML;
		
		public EmailThread(String to, String subject, String message, String user, String password, boolean isHTML){
			this.to = to;
			this.subject = subject;
			this.message = message;
			this.user = user;
			this.password = password;
			this.isHTML = isHTML;
		}
		
		@Override
		public void run() {
			try {
				EmailUtil.send(to, subject, message, user, password, isHTML);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return;
		}
		
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void sendWithHTMLTemplate(String to, String subject,  String user, String pass, String fileName, HashMap<String, String> parameters)throws Exception{
		String l_message = "";
		try {
			BufferedReader l_reader = new BufferedReader(new FileReader(fileName));
			String l_line = "";
			while((l_line = l_reader.readLine()) != null){
				l_message += l_line;
			}
			l_reader.close();
			Iterator l_Iterator = parameters.entrySet().iterator();
			while(l_Iterator.hasNext()){
				Map.Entry<String, String> l_pair = (Map.Entry<String, String>)l_Iterator.next();
				l_message = l_message.replace(l_pair.getKey(), l_pair.getValue());
			}
			EmailThread l_thread = new EmailThread(to, subject, l_message, user, pass, true);
			l_thread.start();
		} catch (Exception e) {
			throw e;
		} 
	}
    
    public static void send(String to, String subject, String body, final String user, final String pass, boolean isHTML) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.post", "587");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pass);
            }
        });
        try {
            MimeMessage l_message = new MimeMessage(session);
            l_message.setFrom(new InternetAddress(user));
            l_message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            l_message.setSubject(subject);
            if(isHTML)
            	l_message.setText(body, "utf-8", "html");
            else
            	l_message.setText(body);
            Transport.send(l_message);
        } catch (MessagingException e) {
            throw e;
        }
    }
}
